﻿using AutoMapper;
using B.W.passport.Application.DTO;
using B.W.passport.BaseRepisitory;
using B.W.passport.Domain;
using B.W.passport.ErrorCode;
using B.W.passport.IBaseRepisitory;
using Microsoft.Data.SqlClient.Server;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.W.passport.Application.AdminService
{
    public class ListService:IListService
    {
        /// <summary>
        /// 仓储-商品列表
        /// </summary>
        private readonly IBaseRepisitory<Product> ProductRepository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="productRepository"></param>
        /// <param name="mapper"></param>
        public ListService(IBaseRepisitory<Product> productRepository, IMapper mapper)
        {
            ProductRepository = productRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public async Task<APIResult<APIPaging<Product>>> GetProduct(int PageIndex, int PageSize)
        {
            APIResult<APIPaging<Product>> result = new APIResult<APIPaging<Product>>();
            result.Code = APIEnumsCode.查询成功;
            result.Message = APIEnumsCode.查询成功.ToString();

            var list =  ProductRepository.GetAll().ToList();

            APIPaging<Product> pIResult = new APIPaging<Product>();

            pIResult.RecordCount = list.Count();
            pIResult.PageCount = (int)Math.Ceiling((pIResult.RecordCount * 1.0) / PageSize);
            pIResult.list = list.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();

            //// 假设存在一个映射方法将 Product 转换为 ProductDTO
            //var productDTOList = pagedList.Select(p => new ProductDTO
            //{

            //    ProductCode=p.ProductCode

            //    // 这里需要实现具体的属性映射
            //}).ToList();

            
            result.Data = pIResult;

            return result;
        }

        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<APIResult<Product>> GetProductById(int id)
        {
            APIResult<Product> result = new APIResult<Product>();
            result.Code = APIEnumsCode.查询成功;
            result.Message = APIEnumsCode.查询成功.ToString();

            var list = ProductRepository.GetAll().ToList();
            var list1 =  list.FirstOrDefault(p => p.ProductId == id);
            
            result.Data = list1;

            return result;
        }
    }
}

